<?php
/**
 * Pertyler
 * Table Browser Page
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require 'main.php';

$treeView = new \KoolTreeView('treeView');

$treeView->imageFolder = $iconsFolder;

$treeView->styleFolder = 'default/';

$treeView->showLines = true;

$root = $treeView->getRootNode();

$root->text = $page;

$root->expand = true;

$root->image = 'ada.gif';

$root->showPlusMinus = false;

function addTable(Pertyler_Table $table)
{
	global $treeView;

	if(file_exists($treeView->imageFolder . $table->Schema() . '.gif'))
		$icon = $table->Schema() . '.gif';
	else
		$icon = 'public.gif';

	if($table->SelectPrivilegeArray())
	{
		if($table->FileTable())
			$link = new Link('files.php',
					'body');
		else
			$link = new Link('table.php',
					'body');

		$link->Table = $table->id();

		$treeView->Add($table->SelectParentTable() ? $table->SelectParentTable()->backgroundName() : 'root',
				$table->backgroundName(),
				$link->Html($table),
				$table->SelectParentTable() ? true : false,
				$icon);
	}
	else
		$treeView->Add($table->SelectParentTable() ? $table->SelectParentTable()->backgroundName() : 'root',
				$table->backgroundName(),
				$table,
				$table->SelectParentTable() ? true : false,
				$icon);

	foreach($table->SelectTableArray() AS $aTable)
		addTable($aTable);

	$typeTable = $table->SelectTypeTable();

	if($typeTable)
		if($typeTable->SelectPrivilegeArray())
		{
			$link = new Link('table.php',
					'body');
	
			$link->Table = $typeTable->id();

			$treeView->Add($table->backgroundName(),
					$typeTable->backgroundName(),
					$link->Html($typeTable),
					false,
					$icon);
		}
		else
			$treeView->Add($table->backgroundName(),
					$typeTable->backgroundName(),
					$typeTable,
					false,
					$icon);
}

$tables = Pertyler_Table::SelectTopLevelTableArray();

foreach($tables AS $table)
	addTable($table);

$hTML = '
<div style="padding: 10px;" >
	' . $treeView->Render() . '
</div>';

echo $page->HTML($hTML);